#include "opencv2/imgproc/imgproc.hpp"
#include <highgui.h>
#include "stdio.h"

#define SIGMA_85  0
#define SIGMA_100 1

int gerade(IplImage * img, double a, double b, int gray)
{
	//y=ax+b
    int width = img->width+1;
    int height = img->height;
	int i;
    for(i=0;i<width;i++)
    {
        if (((i*a+b)*width + i)<(width*height))
            img->imageData[(int)((i*a+b)*width + i)] = 255;
    }
    return 0;
}

void gaussFilter_sigma85(IplImage * img, uchar sigma)
{
	int maske = 5;
    int breite = img->width+1;
    int height = img->height+1;
	int* kernel;  
	int kernel_85[] = {1,4,7,4,1,4,16,26,16,4,7,26,41,26,7,4,16,26,16,4,1,4,7,4,1};
	int kernel_100[] = {0,1,2,1,0,1,4,8,4,1,2,8,16,8,2,1,4,8,4,1,0,1,2,1,0};
	int factor;
	if (sigma == SIGMA_100)
	{
		kernel = kernel_100;
		factor = 80;
	} else if (sigma == SIGMA_85)
	{
		kernel = kernel_85;
		factor = 273;
	}

uint i,j;
///////////////
 int k,l=0;
	int sum=0;
	for(k=0;k<maske*maske;k++)
	{
		sum += kernel[k]*( (unsigned char) img->imageData[k/maske*(breite+1) +
		k%maske]);
		printf(" %d,",kernel[k]*( (unsigned char)
		img->imageData[(k/maske)*(breite+1) + k%maske]));
		if(k%maske==maske-1)
			printf("\n");
	}
    printf("\n%u\n", (unsigned char) (sum/factor));
	////////////////
 for (i=maske/2; i<breite-maske/2 ; i++)
 {
   for (j=maske/2; j<height-maske/2; j++)
   {
    int k,l=0;
	int sum=0;
	for(k=0;k<maske*maske;k++)
	{
		sum += kernel[k]*((unsigned char) img->imageData[(i+k/maske-maske/2)*(breite+1)
		+ k%maske + j-maske/2]);
	}
    img->imageData[i*(breite+1) + j] = (unsigned char) (sum/factor);
   }
 }
    return;
}

void boxFilter(IplImage * img, uchar maske)
{
//y=ax+b/20

    int breite = img->width+1;
    int height = img->height+1;
uint i,j;

 for (i=maske/2; i<breite-maske/2 ; i++)
 {
   for (j=maske/2; j<height-maske/2; j++)
   {
    int k,l;
	int sum=0;
    for(k=0; k<maske;k++)
		for(l=0;l<maske;l++)
		{
			sum += (unsigned char) img->imageData[(i+k-maske/2)*(breite+1) + (j+l-maske/2)];
		}
	   	img->imageData[i*(breite+1) + j] = (unsigned char) (sum/(maske*maske));
   }
 }
    return;
}

int main (int argc , char * argv []) {

 IplImage * img = cvLoadImage (argv [1], CV_LOAD_IMAGE_GRAYSCALE) ;
 if (! img ) {
	printf(" Could not load image file : %s\n", argv [1]);
    exit ( EXIT_FAILURE );
 }

/* get properties, needed to create dest image */
int width     = img->width;
int height    = img->height;
int depth     = img->depth;
int nchannels = img->nChannels;
 
 /* create destination image */
 IplImage *img1 = cvCreateImage( cvSize( width, height ), depth, nchannels );

 printf("Grauwerte von 0x100: %u \n", (unsigned char) img->imageData[100]);
cvCopy(img, img1, NULL );

int k = 100;

int breite = img->width+1;
int i,j;
 for (i=0; i<2; i++)
 {
   for (j=0; j<2; j++)
   {
      img->imageData[i*(breite+1) + j] = 0;
   }
 }

//gerade(img, 0, img->height, 255);
//Canny(img, img, 30, 200, 3);


//cvSaveImage("lena2.jpg",img);

 cvNamedWindow (" original ", CV_WINDOW_AUTOSIZE );
 cvShowImage (" original ", img);

//boxFilter(img, 5);
gaussFilter_sigma85(img, SIGMA_100);
gaussFilter_sigma85(img1,SIGMA_85);

 printf("____Grauwerte von 0x900*3: %u, %u \n", (unsigned char)
 img->imageData[3*900], (unsigned char) img1->imageData[3*900]);
/* result = surfer + milkyway (NULL mask)*/
	cvSub(img, img1, img1,NULL);
//  cvAbsDiffS(img1, img1, cvScalarAll(0));
 printf("Grauwerte von 0x900: %u \n", (unsigned char) img1->imageData[3*900]);

cvNamedWindow("gefiltert", CV_WINDOW_AUTOSIZE );
cvShowImage("gefiltert", img1);

cvWaitKey (0);

cvDestroyWindow("gefiltert");
cvDestroyWindow (" original ");
 cvReleaseImage (& img);


 return EXIT_SUCCESS ;
}
